﻿package co.th.bjc.md.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import co.th.bjc.md.dao.CustomerData;
import co.th.bjc.md.dao.Hospital;
import co.th.bjc.md.dao.HospitalDepartment;
import co.th.bjc.md.process.service.CallService;
import co.th.bjc.md.service.interfaces.CustomerDataManageService;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class CustomerDataManageServlet
 */
public class CustomerDataManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerDataManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html; charset=UTF-8");
		JSONArray jsonObject = new JSONArray();
		boolean status = false;
		String action = request.getParameter("action")==null?"":request.getParameter("action");
		
		if(action.equals("SEARCH")){
			PrintWriter out = response.getWriter();
			jsonObject = this.getSearchResult(request, response);
			out.print(jsonObject);
		}else if(action.equals("DELETE")){
			// Delete process
			if(this.delete(request, response)){
				PrintWriter out = response.getWriter();
				status = true;
				jsonObject = this.getSearchResult(request, response);
				jsonObject.add(status);
				out.print(jsonObject);
			}
		}else if(action.equals("GETEDIT")){
			PrintWriter out = response.getWriter();
			jsonObject = getEditItem(request, response);
			status = true;
			jsonObject.add(status);
			out.print(jsonObject);
		}else if(action.equals("GETVIEW")){
			PrintWriter out = response.getWriter();
			jsonObject = getViewItem(request, response);
			status = true;
			jsonObject.add(status);
			out.print(jsonObject);
		}else if(action.equals("GETHOSPITAL")){
			PrintWriter out = response.getWriter();
			int regionId = Integer.parseInt(request.getParameter("id"));
			jsonObject = getHospitalList(regionId);
			out.print(jsonObject);
		}else if(action.equals("GETHOSPITALDEPARTMENT")){
			PrintWriter out = response.getWriter();
			String hospitalCode = request.getParameter("id");
			jsonObject = getHospitalDepartmentList(hospitalCode);
			out.print(jsonObject);
		}else if(action.equals("GETHOSPITALDEPARTMENTDETAIL")){
			PrintWriter out = response.getWriter();
			String hospitalCode = request.getParameter("hospitalCode");
			String departmentCode = request.getParameter("departmentCode");
			jsonObject = getHospitalDepartmentDetail(hospitalCode, departmentCode);
			out.print(jsonObject);
		}else{
			getServletContext().getRequestDispatcher("/jsp/datamanage/manage_customerdata_main.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private JSONArray getSearchResult(HttpServletRequest request, HttpServletResponse response) {
		String type = request.getParameter("srh_type");
		String department = request.getParameter("srh_department");
		int minBed = Integer.parseInt(request.getParameter("srh_bed_min"));
		int maxBed = Integer.parseInt(request.getParameter("srh_bed_max"));
		int presidentImpRate = Integer.parseInt(request.getParameter("srh_imp_rate_president"));
		int presidentRpRate = Integer.parseInt(request.getParameter("srh_rp_rate_president"));
		int directorImpRate = Integer.parseInt(request.getParameter("srh_imp_rate_director"));
		int directorRpRate = Integer.parseInt(request.getParameter("srh_rp_rate_director"));
		int limitFrom = Integer.parseInt(request.getParameter("limitFrom"));
		int limitTo = Integer.parseInt(request.getParameter("limitTo"));
		
		CustomerDataManageService services = (CustomerDataManageService) CallService.getService("customerdatamanage");
		List<CustomerData> list = services.getCustomerDataList(type, department, minBed, maxBed, presidentImpRate, presidentRpRate, directorImpRate, directorRpRate, limitFrom, limitTo);
		JSONArray jsonObject = JSONArray.fromObject(list);
		return jsonObject;
	}
	
	private int getTotalRecord(){
		return 0;
	}
	
	private boolean insert(HttpServletRequest request, HttpServletResponse response) {
		return false;
	}
	
	private boolean update(HttpServletRequest request, HttpServletResponse response) {
		return false;
	}
	
	private boolean delete(HttpServletRequest request, HttpServletResponse response){
		return false;
	}
	
	private JSONArray getEditItem(HttpServletRequest request, HttpServletResponse response){
		BigInteger id = BigInteger.valueOf(Long.parseLong(request.getParameter("id")));
		CustomerDataManageService services = (CustomerDataManageService) CallService.getService("customerdatamanage");
		CustomerData obj = services.getEditCustomerData(id);
		JSONArray json = JSONArray.fromObject(obj);
		return json;
	}
	
	private JSONArray getViewItem(HttpServletRequest request, HttpServletResponse response){
		BigInteger id = BigInteger.valueOf(Long.parseLong(request.getParameter("id")));
		CustomerDataManageService services = (CustomerDataManageService) CallService.getService("customerdatamanage");
		CustomerData obj = services.getViewCustomerData(id);
		JSONArray json = JSONArray.fromObject(obj);
		return json;
	}
	
	private JSONArray getHospitalList(int regionId){
		CustomerDataManageService services = (CustomerDataManageService) CallService.getService("customerdatamanage");
		List<Hospital> list = services.getHostpitalList(regionId);
		JSONArray json = JSONArray.fromObject(list);
		return json;
	}
	
	private JSONArray getHospitalDepartmentList(String hospitalCode){
		CustomerDataManageService services = (CustomerDataManageService) CallService.getService("customerdatamanage");
		List<HospitalDepartment> list = services.getDepartmentList(hospitalCode);
		JSONArray json = JSONArray.fromObject(list);
		return json;
	}
	
	private JSONArray getHospitalDepartmentDetail(String hospitalCode, String departmentCode){
		CustomerDataManageService services = (CustomerDataManageService) CallService.getService("customerdatamanage");
		Hospital obj = services.getHospitalDepartmentDetail(hospitalCode, departmentCode);
		JSONArray json = JSONArray.fromObject(obj);
		return json;
	}

}
